<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.longtop.com/intelliweb" prefix="e"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>菜单管理</title>
</head>

<e:View id="view">
    <script type="text/javascript">SkinUtil.getSkin();</script>
    <script type="text/javascript">SkinUtil.getModuleSkin();</script>

<div id="Invisual Controls" style="min-height: 40px; background-color: LemonChiffon;">
	<e:Dataset id="Dataset_Menu" datasetDefinition="DatasetDefinition_sys.dsd" datasetRef="Dataset_Menu" pageSize="-1" loadDataCommand="Command_Load" onAfterScroll="Dataset_Menu_onAfterScroll(dataset)">
		<e:Field name="menuId" dataType="string" label="菜单ID" maxLength="32" />
		<e:Field name="menuName" dataType="string" label="菜单名称" maxLength="100" />
		<e:Field name="parentMenuId" dataType="string" label="上级菜单ID" maxLength="32"  readOnly="true"/>
		<e:Field name="menuUrl" dataType="string" label="菜单URL" maxLength="200" />
		<e:Field name="isRelativePath" dataType="string" label="是否相对路径" maxLength="1" />
		<e:Field name="imageUrl" dataType="string" label="菜单图片Url" maxLength="200" />
		<e:Field name="isEnabled" dataType="string" label="是否启用" maxLength="1" />
		<e:Field name="isVisible" dataType="string" label="是否显示" maxLength="1" />
		<e:Field name="target" dataType="string" label="菜单URL目标" maxLength="32" />
		<e:Field name="sortNo" dataType="integer" label="排序号" maxLength="4" />
		<e:Field name="command" dataType="string" label="关联命令对象ID" maxLength="100" />
		<e:Field name="tips" dataType="string" label="菜单提示信息" maxLength="100" />
		<e:Field name="onclick" dataType="string" label="单击菜单调用方法" maxLength="100" />
		<e:Field name="remark" dataType="string" label="备注" maxLength="400" />
	</e:Dataset> 
	
	<e:ListDropDown id="ListDropDown_Boolean" autoDropDown="true" fixed="true">
		<e:ListDropDownItem name="Item1" label="是" value="1"></e:ListDropDownItem>
		<e:ListDropDownItem name="Item2" label="否" value="0"></e:ListDropDownItem>
	</e:ListDropDown> 
	<e:ListDropDown id="ListDropDown_Target" autoDropDown="true" fixed="true">
		<e:ListDropDownItem name="Item1" label="_blank" value="_blank"></e:ListDropDownItem>
		<e:ListDropDownItem name="Item2" label="_self" value="_self"></e:ListDropDownItem>
		<e:ListDropDownItem name="Item3" label="_parent" value="_parent"></e:ListDropDownItem>
		<e:ListDropDownItem name="Item4" label="_top" value="_top"></e:ListDropDownItem>
	</e:ListDropDown> 
	
	<e:Command id="Command_Save" action="com.longtop.framework.sys.menu.web.MenuListAction"	method="saveMenu">
		<e:DatasetInfo name="info1" dataset="Dataset_Menu"></e:DatasetInfo>
	</e:Command> 
	<e:Command id="Command_Load" action="com.longtop.framework.sys.menu.web.MenuListAction" method="loadMenu" showHintInfoOnSuccess="false">
		<e:DatasetInfo name="info1" dataset="Dataset_Menu" alwayRefreshClient="true" submitType="none"></e:DatasetInfo>
	</e:Command>  
	<e:Command id="Command_HasChild" action="com.longtop.framework.sys.menu.web.MenuListAction" method="hasChild" showHintInfoOnSuccess="false">
		<e:Parameter name="id"></e:Parameter>
	</e:Command>
</div>

<body>
	
<e:Page id="ePage" action="com.longtop.framework.sys.menu.web.MenuListAction" style="min-height: 80px; width: 100%; height: 100%;" onAfterLoadPage="ePage_onAfterLoadPage()">
	<table style="width: 100%; height: 100%;" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
		<tr>
			<td align="left" valign="top">
				<e:Panel id="Panel_Tree" style="width: 285px; height: 100%" caption="菜单树" captionIcon="/images/query_win.gif" captionStyle="color:#888888" allowFold="true" showArrowButton="false">
					<table style="text-align: left; width: 100%;" border="0" cellpadding="3" cellspacing="3" width="100%" align="center">
						<tr>
							<td align="center">
								<e:Button id="Button_Add" caption="新增" style="height: 22px; width: 70px" icon="/images/add.gif" onClick="Button_Add_onClick()"></e:Button>&nbsp;&nbsp;
								<e:Button id="Button_Delete" caption="删除" style="height: 22px; width: 70px" icon="/images/delete.gif" onClick="Button_Delete_onClick()"></e:Button>&nbsp;&nbsp;
								<e:Button id="Button_Save" caption="保存" style="width: 70px; height: 22px" icon="/images/submit.gif" onClick="Button_Save_onClick()"></e:Button>
							</td>
						</tr>
						<tr>
							<td align="left">
								<e:DataTree id="DataTree_Menu" dataset="Dataset_Menu" nameField="menuId" valueField="menuId" 
									parentField="parentMenuId" labelField="menuName" style="border-color:#3ABCFA;width: 100%; height: 390; text-align: left" 
									showRoot="true" rootLabel="菜单树" lazyLoading="true" useDefaultFolderIcon="true" contextMenuItems="expand,collapse">
								</e:DataTree>
							</td>
						</tr>
					</table>
				</e:Panel>
			</td>
			<td valign="top" width="100%">
				<e:Panel id="Panel_Result" style="width: 100%; height: 100%" captionStyle="color:#888888" caption="菜单明细信息" captionIcon="/images/list_win.gif" showArrowButton="false">
					<table style="text-align: left; width: 100%;" border="0" cellpadding="3" cellspacing="3">
					  <tbody id="menuInfo" style="display: block">
						<tr>
							<td width="130px" align="right">菜单编号</td>
							<td><e:Text id="id" dataset="Dataset_Menu" field="menuId" style="width: 350px"></e:Text>
								<font color="red">*</font>
							</td>
						</tr>
						<tr>
							<td width="130px" align="right">菜单名称</td>
							<td><e:Text id="name" dataset="Dataset_Menu" field="menuName" style="width: 350px"></e:Text>
								<font color="red">*</font></td>
						</tr>
						<tr>
							<td align="right">上级菜单编号</td>
							<td><e:Text id="parentId" dataset="Dataset_Menu" field="parentMenuId" style="width: 350px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">菜单路径</td>
							<td><e:Text id="url" dataset="Dataset_Menu" field="menuUrl" style="width: 350px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">是否相对路径</td>
							<td><e:Text id="isRelativePath" dataset="Dataset_Menu" field="isRelativePath" dropDown="ListDropDown_Boolean" style="width: 335px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">菜单路径所在框架名</td>
							<td><e:Text id="menuTarget" dataset="Dataset_Menu" field="target" dropDown="ListDropDown_Target" style="width: 335px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">菜单图片</td>
							<td><e:Text id="image" dataset="Dataset_Menu" field="imageUrl" style="width: 350px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">同级菜单顺序号</td>
							<td><e:Text id="sortNo" dataset="Dataset_Menu" field="sortNo" style="width: 350px"></e:Text>
								<font color="red">*</font></td>
						</tr>
						<tr>
							<td align="right">关联命令对象ID</td>
							<td><e:Text id="command" dataset="Dataset_Menu" field="command" style="width: 350px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">是否启用</td>
							<td><e:Text id="isEnabled" dataset="Dataset_Menu" field="isEnabled" dropDown="ListDropDown_Boolean" style="width: 335px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">是否可视</td>
							<td><e:Text id="isVisible" dataset="Dataset_Menu" field="isVisible" dropDown="ListDropDown_Boolean" style="width: 335px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">菜单提示信息</td>
							<td><e:Text id="tips" dataset="Dataset_Menu" field="tips" style="width: 350px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">单击菜单调用方法</td>
							<td><e:Text id="onclick" dataset="Dataset_Menu" field="onclick" style="width: 350px"></e:Text></td>
						</tr>
						<tr>
							<td align="right">备注</td>
							<td><e:Text id="remark" dataset="Dataset_Menu" field="remark" style="width: 350px"></e:Text></td>
						</tr>
						</tbody>
					</table>
				</e:Panel>
			</td>
		</tr>
	</table>
</e:Page>

</body>
</e:View>
</html>
	
<script type="text/javascript">

//init
function ePage_onAfterLoadPage(){
	var tree = $J("DataTree_Menu");
    if (!tree) {
        MsgBox.showWarnMsg("菜单树还未生成！");
        return;
    }
    if (!tree.currentNode) {
    	tree.setCurrentNode();
	}
    tree.expandNode();//展开当前节点
	document.getElementById("menuInfo").style.display = "none";
}
//判断是否是新增的节点，若是则改变field和文本框为可编辑。否则不可编辑	
function Dataset_Menu_onAfterScroll(dataset){
	if(dataset.currentRecord != null){
		document.getElementById("menuInfo").style.display = "block";
		var state = dataset.currentRecord.state;
		if (state == Constant.RECORD.STATE_INSERT || state == Constant.RECORD.STATE_NEW) {
			parentId_Syn(dataset.currentRecord);	//新增记录，自动关联上级菜单编号：
			var field = dataset.getField("menuId");
			field.setReadOnly(false);
		}else{
			var field = dataset.getField("menuId");
			field.setReadOnly(true);
		}	
	}else{
		document.getElementById("menuInfo").style.display = "none";
	}
}

//增加
function Button_Add_onClick(){
	var menu = $J(DataTree_Menu);
    if (!menu) 
    	return;
    menu.insertChildNode();
    var _dataset = $J(Dataset_Menu);
    //新增时，给contextRelative,menuEnabled,menuVisible赋默认值
	_dataset.setValue("isRelativePath","1");
	_dataset.setValue("isEnabled","1");
	_dataset.setValue("isVisible","1");
}

//删除
function Button_Delete_onClick(){
	var tree = $J(DataTree_Menu);
	
	//如果树对象不存在，则返回
    if (!tree) {
        MsgBox.showWarnMsg("菜单树还未生成！");
        return;
    }
	if (!tree.currentNode) {
		MsgBox.showWarnMsg("请选择结点！");
		return;
	}
	if (tree.currentNode.isVirtualRoot) {
		MsgBox.showWarnMsg("不能删除根结点！");
		return;
	}	
		
	//先从客户端树结点集对象中，判断当前结点是否有下级结点。
    if (tree.currentNode.childNodes && tree.currentNode.childNodes.length > 0) {
        MsgBox.showWarnMsg("当前菜单有下级菜单，不能删除！","");
        return;
    }

    //去服务器验证	【注意】
	var menuId = tree.currentNode.getName();//tree设置了nameFiled属性。对应了id。
	Command_HasChild.parameters.setValue("id",menuId);
	Command_HasChild.execute();
	
	var hasChild=Command_HasChild.parameters.getParameter("hasChild").value;
	if(hasChild == "true"){
		MsgBox.showWarnMsg("当前菜单有下级菜单，不能删除。","");
		return;
	}
	//删除节点:
	MsgBox.showConfirmMsg("您确定要删除菜单[" + tree.currentNode.getLabel() + "]吗？","",[doDelete,tree]);
}
//确定删除
function doDelete(tree){
	var dataset = $J(Dataset_Menu);
    var state = dataset.currentRecord.state;
    tree.deleteNode();//先客户端删除记录
    //如果不是本次新增的，则再去服务器上删数据库记录。
    if (state != "1" &&  state != "4") {
        Command_Save.execute();
   	}
}

//保存
function Button_Save_onClick(){
	//必输项校验
	var record = Dataset_Menu.getFirstRecord();
	while(record){
		if(record.state != 0){//正常状态的跳过
			var id = record.getValue("menuId").Trim();
			if(isEmpty(id)){
				MsgBox.showWarnMsg('菜单编号不能为空。');
				return false;
			}
			var name = record.getValue("menuName").Trim();
			if(isEmpty(name)){
				MsgBox.showWarnMsg('菜单编号[ ' + id + ' ]的菜单名称不能为空。');
				return false;
			}
			var sortNo = record.getValue("sortNo")+"";
			if(isEmpty(sortNo)){
				MsgBox.showWarnMsg('菜单编号[ ' + id + ' ]的菜单排序号不能为空。');
				return false;
			}
			//菜单编号不能和上级菜单编号一样
			var parentId = record.getValue("parentMenuId");
			if(id == parentId){
				MsgBox.showWarnMsg('菜单编号[ ' + id + ' ]不能和上级菜单编号一样。');
				return false;
			}
			if(record.state == 1 || record.state == 4){
				parentId_Syn(record);	//若是新增的记录，则自动关联上级菜单编号：
			}
		}
		record =  record.getNextRecord();
	}
	
	var flag = Command_Save.execute();
	if(flag){
		Dataset_Menu.getField("menuId").setReadOnly(true);//保存成功后，ID字段只读。
	}
}
//自动关联上级菜单编号：
function parentId_Syn(crecord){
	var tree = $J("DataTree_Menu");
	var parentRecord = tree.getNodeByRecord(crecord).parentNode.record;
	if(parentRecord){
		//alert(parentRecord.getValue("id"));
		crecord.setValue("parentId",parentRecord.getValue("menuId"));
	}
}
</script>


